//
//  CouponMovieListCell.m
//  Kokozu
//
//  Created by Bismarrck on 11-7-16.
//  Copyright 2011年 Tsinghua University. All rights reserved.
//

#import "CouponMovieListCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation CouponMovieListCell

- (void)drawRect:(CGRect)rect {
    timeCounterDisplay = [[UILabel alloc] initWithFrame:CGRectMake(37, 20, 160, 20)];
    timeCounterDisplay.font = [UIFont boldSystemFontOfSize:14];
    timeCounterDisplay.textColor = [UIColor whiteColor];
    timeCounterDisplay.backgroundColor = [UIColor darkGrayColor];
    timeCounterDisplay.alpha = 0.7;
    timeCounterDisplay.textAlignment = UITextAlignmentCenter;
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
    [self addSubview:timeCounterDisplay];
}


#pragma mark -
#pragma mark Time Counter

- (void)timerFireMethod:(NSTimer*)theTimer
{
    //NSDateFormatter *dateformatter = [[[NSDateFormatter alloc]init] autorelease];//定义NSDateFormatter用来显示格式
    //[dateformatter setDateFormat:@"yyyy MM dd hh mm ss"];//设定格式
    NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象
    NSDateComponents *timeCounter = [[NSDateComponents alloc] init];//初始化目标时间（好像是世博会的日期）
    [timeCounter setYear:2011];
    [timeCounter setMonth:8];
    [timeCounter setDay:1];//使用NSTimer实现倒计时
    [timeCounter setHour:9];
    [timeCounter setMinute:30];
    [timeCounter setSecond:0];
    
    NSDate *todate = [cal dateFromComponents:timeCounter];//把目标时间装载入date
    [timeCounter release];
    // NSString *ssss = [dateformatter stringFromDate:dd];
    // NSLog([NSString stringWithFormat:@"shibo shi:%@",ssss]);
    
    NSDate *today = [NSDate date];//得到当前时间
    // NSString *sss = [dateformatter stringFromDate:today];
    // NSLog([NSString stringWithFormat:@"xianzai shi:%@",sss]);
    //用来得到具体的时差
    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0];
    timeCounterDisplay.text = [NSString stringWithFormat:@"倒计时:%d时%d分%d秒",[d hour], [d minute], [d second]];
}

- (void)setMoviePoster:(UIImage *)aMoviePoster
{
    [super setMoviePoster:aMoviePoster];
    moviePosterView.image = aMoviePoster;
}

- (void)setMovieName:(NSString *)aMovieName
{
    [super setMovieName:aMovieName];
    movieNameLabel.text = aMovieName;
}

- (void)setMovieOffPrice:(int)theMovieOffPrice
{
    [super setMovieOffPrice:theMovieOffPrice];
    movieOffPriceLabel.text = [NSString stringWithFormat:@"%d %d", theMovieOffPrice/10, theMovieOffPrice%10];
}

@end


@implementation CouponMovieListCellExpanded

@synthesize imageConponent;
@synthesize imageConponent_two,imageConponent_three;

- (void)drawRect:(CGRect)rect {
    timeCounterDisplay = [[UILabel alloc] initWithFrame:CGRectMake(37, 0, 160, 20)];
    timeCounterDisplay.font = [UIFont boldSystemFontOfSize:14];
    timeCounterDisplay.textColor = [UIColor whiteColor];
    timeCounterDisplay.backgroundColor = [UIColor darkGrayColor];
    timeCounterDisplay.alpha = 0.7;
    timeCounterDisplay.textAlignment = UITextAlignmentCenter;
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
    [imageConponent_three.view addSubview:timeCounterDisplay];
}


#pragma mark -
#pragma mark Time Counter

- (void)timerFireMethod:(NSTimer*)theTimer
{
    //NSDateFormatter *dateformatter = [[[NSDateFormatter alloc]init] autorelease];//定义NSDateFormatter用来显示格式
    //[dateformatter setDateFormat:@"yyyy MM dd hh mm ss"];//设定格式
    NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象
    NSDateComponents *timeCounter = [[NSDateComponents alloc] init];//初始化目标时间（好像是世博会的日期）
    [timeCounter setYear:2011];
    [timeCounter setMonth:8];
    [timeCounter setDay:1];//使用NSTimer实现倒计时
    [timeCounter setHour:9];
    [timeCounter setMinute:30];
    [timeCounter setSecond:0];
    
    NSDate *todate = [cal dateFromComponents:timeCounter];//把目标时间装载入date
    [timeCounter release];
    // NSString *ssss = [dateformatter stringFromDate:dd];
    // NSLog([NSString stringWithFormat:@"shibo shi:%@",ssss]);
    
    NSDate *today = [NSDate date];//得到当前时间
    // NSString *sss = [dateformatter stringFromDate:today];
    // NSLog([NSString stringWithFormat:@"xianzai shi:%@",sss]);
    //用来得到具体的时差
    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0];
    timeCounterDisplay.text = [NSString stringWithFormat:@"倒计时:%d时%d分%d秒",[d hour], [d minute], [d second]];
}

- (void)spinLayer:(CALayer *)inLayer duration:(CFTimeInterval)inDuration
        direction:(float)direction
{
    inLayer.anchorPoint = CGPointMake(0, 0);
    
    CABasicAnimation* rotationAnimation;
    
    // Rotate about the z axis
    rotationAnimation = 
    [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    // Rotate 360 degress, in direction specified
    /*rotationAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(3.14125, 0, 0,direction)];
     */
    
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 * direction];
    
    // Perform the rotation over this many seconds
    rotationAnimation.duration = inDuration;
    
    // Set the pacing of the animation
    rotationAnimation.timingFunction = 
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    rotationAnimation.fillMode = kCAFillModeForwards;
    rotationAnimation.removedOnCompletion = NO;
    
    // Add animation to the layer and make it so
    [inLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
}

- (void) setRotatedPoster:(UIImage *)rotatedPoster {
    [super setRotatedPoster:rotatedPoster];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 192, 80)];
    imageView.image = rotatedPoster;
    self.imageConponent_three = [[CouponImageConponentView alloc] init];
    [imageConponent_three.view addSubview:imageView];
    imageConponent_three.view.frame = CGRectMake(-100, 17, 233, 110);
    [self spinLayer:imageConponent_three.view.layer duration:0.6 direction:0.05];
    [self addSubview:imageConponent_three.view];
    NSLog(@"setposter");
}


- (void)setCinemaName:(NSString *)aCinemaName
{
    UILabel *cinemaNameLabel_new = [[UILabel alloc] initWithFrame:CGRectMake(25, 15, 188, 20)];
    cinemaNameLabel_new.textAlignment = UITextAlignmentLeft;
    cinemaNameLabel_new.text = [NSString stringWithFormat:@"中关村美嘉", aCinemaName];
    cinemaNameLabel_new.backgroundColor = [UIColor clearColor];
    [super setCinemaName:aCinemaName];
    imageConponent = [[CouponImageConponentView alloc] init];
    imageConponent.view.frame = CGRectMake(-117, -47, 233, 110);
    [imageConponent.view addSubview:cinemaNameLabel_new];
    [self spinLayer:imageConponent.view.layer duration:0.6 direction:0.00];
    [self addSubview:imageConponent.view];
    
    
}

- (void)setTicketsNum:(int)theTicketsNum
{
    UILabel *ticketsNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, 188, 20)];
    ticketsNumberLabel.textAlignment = UITextAlignmentRight;
    ticketsNumberLabel.text = [NSString stringWithFormat:@"剩余%d张", theTicketsNum];
    ticketsNumberLabel.backgroundColor = [UIColor clearColor];
    [super setTicketsNum:theTicketsNum];
    imageConponent_two = [[CouponImageConponentView alloc] init];
    imageConponent_two.view.frame = CGRectMake(-110, -17, 233, 110);
    [imageConponent.view addSubview:ticketsNumberLabel];
    [self spinLayer:imageConponent_two.view.layer duration:0.6 direction:0.025];
    [self addSubview:imageConponent_two.view];
}

- (void)setMovieName:(NSString *)movieName {
    [super setMovieName:movieName];
    UILabel *movieNameLabel_new = [[UILabel alloc] initWithFrame:CGRectMake(25, 15, 188, 20)];
    movieNameLabel_new.text = [NSString stringWithFormat:@"%@",movieName];
    movieNameLabel_new.backgroundColor = [UIColor clearColor];
    [imageConponent_two.view addSubview:movieNameLabel_new];
}

- (void)setMovieTime:(NSString *)aMovieTime
{
    [super setMovieTime:aMovieTime];
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 15, 188, 20)];
    timeLabel.text = [NSString stringWithFormat:@"%@",aMovieTime];
    timeLabel.textAlignment = UITextAlignmentRight;
    timeLabel.backgroundColor = [UIColor clearColor];
    [imageConponent_two.view addSubview:timeLabel];
    movieTimeLabel.text = aMovieTime;
}

@end
